#coding: utf-8

class Client 
  attr_reader :tcp, :request_talk, :talk, :ask_file
  attr_accessor :nfk, :pseudo
  
  def initialize(tcp_server, p_pseudo)
    @pseudo = p_pseudo
    @tcp = tcp_server
    @nfk = true 
    
    @request_talk = []
    @talk = []
    
    @ask_file = {}
    @send = {}
    
    @sem_puts = Mutex.new()
  end # initialize
  
  def puts(msg)
   @sem_puts.lock
    @tcp.puts(msg)
   @sem_puts.unlock
  end
  
  def add_request_talk(client)
    @request_talk << client if not @request_talk.include?(client)
  end
  
  def delete_request_talk(client)
    @request_talk.delete(client)
  end
  
  def request_talk?(client)
    @request_talk.include?(client)
  end
  
  def add_talk(client)
    @request_talk.delete(client)
    @talk << client if not @talk.include?(client)
  end
  
  def delete_talk(client)ask_file
    @talk.delete(client)
  end
  
  def talk?(client)
    @talk.include?(client)
  end
  
  
  def add_ask_send(client, fichier)
    @ask_file[client] = [] if not @ask_file[client]
    @ask_file[client] << fichier if not @ask_file[client].include?(fichier)
  end
  
  def delete_ask_send(client, fichier)
    if @ask_file[client]
      @ask_file[client].delete(fichier)
      @ask_file.delete(client) if @ask_file[client].length < 0
    end
  end
  
  def ask_send?(client, fichier)
    @ask_file[client] and @ask_file[client].include?(fichier)
  end
  
  
  
  
  def <=>(other)
    @pseudo.upcase <=> other.pseudo.upcase
  end
  
  def to_s
    @pseudo
  end
  
  
end # client
